package com.ciwong.epaper.modules.me.adapter;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.ciwong.epaper.common.guidedialog.BaseGuideLayerDialog;
import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.me.bean.MaterialBean;
import com.ciwong.epaper.modules.me.bean.SubjectBean;
import com.ciwong.epaper.widget.MaterialItemView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by keepon on 2018/8/7.
 */
public class NewBookDeskAdapter extends RecyclerView.Adapter<NewBookDeskAdapter.NewBookDeskViewHolder> {
    private Context mContext;
    private List<SubjectBean> mSubjectBeanList = new ArrayList<>();
    private List<CatalogueInfo> mCataloguesBeanList = new ArrayList<>();
    private OnDialogCreatedListener onDialogCreatedListener;
    public BaseGuideLayerDialog dialog;
    public BaseGuideLayerDialog dialog2;

    public NewBookDeskAdapter(Context context, List<SubjectBean> subjectBeanList, List<CatalogueInfo> cataloguesBeanList) {
        mContext = context;
        mSubjectBeanList.addAll(subjectBeanList);
        mCataloguesBeanList.addAll(cataloguesBeanList);
    }

    @Override
    public NewBookDeskViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        NewBookDeskViewHolder newBookDeskViewHolder = new NewBookDeskViewHolder(new MaterialItemView(mContext));
        return newBookDeskViewHolder;
    }

    @Override
    public void onBindViewHolder(NewBookDeskViewHolder holder, int position) {
        holder.bindData(position, mSubjectBeanList.get(position), mCataloguesBeanList);
    }

    @Override
    public int getItemCount() {
        return mSubjectBeanList != null ? mSubjectBeanList.size() : 0;
    }

    public void updateData(List<SubjectBean> subjectBeanList, List<CatalogueInfo> cataloguesBeanList) {
        mSubjectBeanList.clear();
        mSubjectBeanList.addAll(subjectBeanList);
        mCataloguesBeanList.clear();
        mCataloguesBeanList.addAll(cataloguesBeanList);
        notifyDataSetChanged();
    }

    public void updateSubjectBeanList(List<SubjectBean> subjectBeanList) {
        mSubjectBeanList.clear();
        mSubjectBeanList.addAll(subjectBeanList);
        notifyDataSetChanged();
    }

    public void updateCatalogueList(List<CatalogueInfo> cataloguesBeanList) {
        mCataloguesBeanList.clear();
        mCataloguesBeanList.addAll(cataloguesBeanList);
        notifyDataSetChanged();
    }

    public class NewBookDeskViewHolder extends RecyclerView.ViewHolder {

        public NewBookDeskViewHolder(View itemView) {
            super(itemView);
        }

        public void bindData(final int position, final SubjectBean subjectBean, final List<CatalogueInfo> cataloguesBean) {

            if (itemView instanceof MaterialItemView) {
                MaterialItemView maerialItemView = (MaterialItemView) itemView;
                maerialItemView.bindData(subjectBean, cataloguesBean);
                maerialItemView.setOnMaterItemViewClicklistener(new MaterialItemView.OnMaterItemViewClicklistener() {
                    @Override
                    public void onClickChangeMoudle(String unitBeanUnitid) {
                        if (mOnMaterItemClicklistener != null) {
                            mOnMaterItemClicklistener.onClickChangeMoudle(position, subjectBean, cataloguesBean, unitBeanUnitid);
                        }
                    }

                    @Override
                    public void onClickChangeMaterial(MaterialBean selectedMaterialBean) {
                        if (mOnMaterItemClicklistener != null) {
                            mOnMaterItemClicklistener.onClickChangeMaterial(position, subjectBean, cataloguesBean, selectedMaterialBean);
                        }
                    }

                    @Override
                    public void onClickStartStudy(CatalogueInfo catalogueInfo) {
                        if (mOnMaterItemClicklistener != null) {
                            mOnMaterItemClicklistener.onClickStartStudy(position, subjectBean, cataloguesBean,catalogueInfo);
                        }
                    }

                    @Override
                    public void onClickRefreshCatalogue() {
                        if (mOnMaterItemClicklistener != null) {
                            mOnMaterItemClicklistener.onClickRefreshCatalogue(position, subjectBean, cataloguesBean);
                        }
                    }
                });
            }
        }
    }

    public void setOnDialogCreatedListener(OnDialogCreatedListener onDialogCreatedListener) {
        this.onDialogCreatedListener = onDialogCreatedListener;
    }

    public interface OnDialogCreatedListener {
        void onCreatedListener(Dialog dialog);
    }

    public void setOnMaterItemClicklistener(OnMaterItemClicklistener onMaterItemClicklistener) {
        mOnMaterItemClicklistener = onMaterItemClicklistener;
    }

    OnMaterItemClicklistener mOnMaterItemClicklistener;

    public interface OnMaterItemClicklistener {
        //切换单元
        void onClickChangeMoudle(int position, SubjectBean subjectBean, List<CatalogueInfo> cataloguesBean, String currentUnitBeanUnitid);

        //选择教材
        void onClickChangeMaterial(int position, SubjectBean subjectBean, List<CatalogueInfo> cataloguesBean, MaterialBean materialBean);

        //点击开始学习
        void onClickStartStudy(int position, SubjectBean subjectBean, List<CatalogueInfo> cataloguesBean,CatalogueInfo catalogueInfo);

        void onClickRefreshCatalogue(int position, SubjectBean subjectBean, List<CatalogueInfo> cataloguesBean);
    }
}
